var flag = true;
layui.use(['form','table', 'layer'],
function() {
	$ = layui.jquery;
	var form = layui.form,
	layer = layui.layer;
    var table = layui.table;

	 //监听提交
	form.on('submit(add)',
	function(data) {
		if(flag)
		{
			flag = false;
			$("#signupForm").ajaxSubmit(function (message) {
				 console.log(message);
                 
                 if(message.code == 10001)
				 {
					layer.msg("执行成功", {
						icon: 6
					},
					function() {
						//关闭当前frame
                        xadmin.close();
                        parent.tableInit();
					});
				 }else{
					layer.msg(message.msg, {
						icon: 5
					},
					function() {
						//关闭当前frame
                        xadmin.close();
					});
				 }
			});
		}else{
			layer.msg('正在提交。。。', {icon: 6});
		}
		return false;
	});
    /*关闭页面*/
    form.on('submit(quxiao)',
    function(data) {
        //关闭当前frame
        xadmin.close();
        return false;
    });
    //监听select
	form.on('select(zcate)',
	function(data) {
		var params = {};
		params.id = data.value;
		$.ajax({
			type: "POST",
			url: "/admin/category/lowerCate.html",
			cache: false, //禁用缓存
			data: params, //传入组装的参数
			dataType: "json",
			success: function (result) {
				console.log(result);
				var str = '<option value="">请选择产品子分类</option>';
				if(result.code==10001){
					var i;
					var info = result.option;
					for (i in info) {
						str += '<option value="'+info[i].category_id+'">'+info[i].category_name+'</option>';
					}
					$('#cat_id').html(str);
					form.render('select');
				}else{
					$('#cat_id').html('<option value="">请选择产品子分类</option>');
					$('#brand_name').html('<option value="">请选择产品品牌</option>');
					$('#memory_name').html('<option value="">请选择产品内存</option>');
					form.render('select');
				}
			},
			error:function () {
				parent.layer.msg('网络错误', {icon: 5});
			}
		})
	});
    
    //监听select
	form.on('select(bute)',
	function(data) {
		var params = {};
		params.id = data.value;
		$.ajax({
			type: "POST",
			url: "/admin/category/lowerBute.html",
			cache: false, //禁用缓存
			data: params, //传入组装的参数
			dataType: "json",
			success: function (result) {
				console.log(result);
				var pingpai = result.pingpai;
				var neicun = result.neicun;
				var i;
				
				var pingpai_str = '<option value="">请选择产品品牌</option>';
				for (i in pingpai) {
					pingpai_str += '<option value="'+pingpai[i]+'">'+pingpai[i]+'</option>';
				}
				$('#brand_name').html(pingpai_str);
				
				var neicun_str = '<option value="">请选择产品内存</option>';
				for (i in neicun) {
					neicun_str += '<option value="'+neicun[i]+'">'+neicun[i]+'</option>';
				}
				$('#memory_name').html(neicun_str);
				
				form.render('select');
			},
			error:function () {
				parent.layer.msg('网络错误', {icon: 5});
			}
		})
	});
    
});
